package com.itheima.domain;

import java.util.ArrayList;
import java.util.Scanner;

public class TestStudent {
    static  ArrayList<Student> list = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //ArrayList<Student> list = new ArrayList<>();
        System.out.println();
        while (true) {
            System.out.println("- - - - - - 欢迎来到学生管理系统 - - - - - -");
            System.out.println("0.查看所有的学生");
            System.out.println("1.添加学生");
            System.out.println("2.根据学号和姓名查看学生");
            System.out.println("3.删除学生");
            System.out.println("4.修改学生");
            System.out.println("5.退出学生管理系统!");
            System.out.println("请输入您的选择:");
            String id = sc.next();
            exit:
            switch (id) {
                case "0":
                    selcetAll(list);
                    break;
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    getStudent(list);
                    break;
                case "3":
                    delete(list);
                    break;
                case "4":
                    setStudent(list);
                    break;
                case "5":
                    System.out.println("- - - - - 感谢您的使用 - - - - -");
                    break exit;
                default:
                    System.out.println("不好意思,您的要求我们暂时还不能满足您,请重新输入:");
            }
        }
    }

    private static void selcetAll(ArrayList<Student> list) {
        System.out.println("      姓名      年龄      年龄      生日      ");
        for (int i = 0; i < list.size(); i++) {
            list.get(i).show();
        }

    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student st = new Student();
        System.out.println("请输入你要添加的学生的学号:");
        String sid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                System.out.println("你输入的学号已经存在了,请重新输入学号:");
                sid = sc.nextLine();
                sid = sc.next();
                i = -1;
            }
        }
        st.setSid(sid);
        System.out.println("请输入你要添加的学生的姓名:");
        st.setName(sc.next());
        System.out.println("请输入你要添加的学生的年龄:");
        st.setAge(sc.nextInt());
        System.out.println("请输入你要添加的学生的生日:");
        st.setBirthday(sc.next());
        list.add(st);
        System.out.println("恭喜您,您成功添加了学生!");
    }

    private static void getStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        while (true) {
            System.out.println("根据学号查找学生,请输入1:");
            System.out.println("根据姓名查找学生,请输入2:");
            String id = sc.next();
            if ("1".equals(id)) {
                System.out.println("请您输入要查看的学生学号:");
                String sid = sc.next();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getSid().equals(sid)) {
                        if (count == 1) {
                            System.out.println("      姓名      年龄      年龄      生日      ");
                            count--;
                        }
                        list.get(i).show();
                    }
                }
                if (count == 1) {
                    System.out.println("不好意思,您查找的学生信息没有找到!");
                }
                break;
            } else if ("2".equals(id)) {
                System.out.println("请您输入要查看的学生姓名:");
                String sname = sc.next();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getName().equals(sname)) {
                        if (count == 1) {
                            System.out.println("      姓名      年龄      年龄      生日      ");
                            count--;
                        }
                        list.get(i).show();
                    }
                }
                if (count == 1) {
                    System.out.println("不好意思,您查找的学生信息没有找到!");
                }
                break;
            }
            System.out.println(" - - - - - 您输入的代码没有对应的功能 - - - - - ");
        }
    }

    private static void delete(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        while (true) {
            System.out.println("根据学号删除学生,请输入1:");
            System.out.println("根据姓名删除学生,请输入2:");
            String id = sc.next();
            if((!"1".equals(id))&&!("2".equals("2"))){
                continue;
            }
            if ("1".equals(id)) {
                System.out.println("请您输入要删除的学生学号:");
                String sid = sc.next();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getSid().equals(sid)) {
                        if (count == 1) {
                            System.out.println("      姓名      年龄      年龄      生日      ");
                            count--;
                        }
                        System.out.println("您删除的学生信息如下:");
                        list.get(i).show();
                        list.remove(i);

                        break;
                    }
                }

            } else if ("2".equals(id)) {
                System.out.println("请您输入要删除的学生姓名:");
                String sname = sc.next();
                for (int i = list.size(); i >= 0; i--) {
                    if (list.get(i).getName().equals(sname)) {
                        if (count == 1) {
                            System.out.println("      姓名      年龄      年龄      生日      ");
                            count--;
                        }
                        System.out.println("您删除的学生信息如下:");
                        list.get(i).show();
                        list.remove(i);
                    }
                }
            }
            if (count == 1) {
                System.out.println("不好意思,您要删除的学生不存在!");
            } else {
                System.out.println("恭喜您,成功删除学生的信息成功!");
            }
            break;
        }

    }

    private static void setStudent(ArrayList<Student> list) {
        int idl;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改学生的学号:");
        Student st = new Student();
        String sid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                System.out.println("找到了你要修改的学生!");
                idl = i;
                System.out.println("请输入修改后的学号:");
                String xsid = sc.next();
                exit:while (true) {
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (list.get(i1).getSid().equals(xsid)) {
                            System.out.println("新学号有冲突,请您重新输入:");
                            xsid = sc.next();
                            break;
                        }
                    }
                    st.setSid(xsid);
                    break exit;
                }
                System.out.println("请输入修改后学生的姓名:");
                String name = sc.next();
                st.setName(name);
                System.out.println("请输入修改后学生的年龄:");
                st.setAge(sc.nextInt());
                System.out.println("请输入修改后学生的生日:");
                st.setBirthday(sc.next());
                list.set(idl, st);
                return;
        }
        }
        System.out.println("不好意思,没有找到您要的学号相关的学生信息!!!");
    }

}
